<template>
  <div class="account-box">
    <el-form
      :model="params"
      status-icon
      :rules="rules"
      ref="ruleForm"
      class="amendWord"
    >
      <el-form-item
        label="原密码"
        prop="oldPassword"
        :label-width="forLabelWidth"
      >
        <el-input
          type="password"
          v-model="params.oldPassword"
          autocomplete="off"
          placeholder="请输入原密码"
          maxlength="32"
          :status-icon="false"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="新密码"
        prop="newPassword"
        :label-width="forLabelWidth"
      >
        <el-input
          type="password"
          v-model="params.newPassword"
          autocomplete="off"
          placeholder="8-32位，至少包含数字、字母、字符2种组合"
          maxlength="32"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="确定新密码"
        prop="newPasswordAgain"
        :label-width="forLabelWidth"
      >
        <el-input
          type="password"
          v-model="params.newPasswordAgain"
          placeholder="请再次输入密码"
          autocomplete="off"
          maxlength="32"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="info" @click="submitForm('ruleForm')"
          >确认提交</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import md5 from "js-md5";
export default {
  name: "AccountSecurity", //账号安全
  data() {
    var validatePass1 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入新密码"));
      } else if (value !== "") {
        const reg =
          /^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?![,\.#\@\$\&\|\?\!\%'\\"/\+\*\~\-\(\)\[\]\{\}\=\<\>:;"^_`]+$)[,\.#\@\$\&\|\?\!\%'\\/\+\*\~\-\(\)\[\]\{\}\=\<\>:;"^_`0-9A-Za-z]{8,32}$/;
        if (reg.test(value)) {
          callback();
        } else {
          callback(
            new Error(
              "*必须包含大小写字母和数字的组合或者符号的组合(必须组合)，可以使用特殊字符，长度在8-32之间"
            )
          );
        }
      } else {
        if (this.params.newPassword !== "") {
          this.$refs.ruleForm.validateField("newPasswordAgain");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.params.newPassword) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      forLabelWidth: "130px",
      params: {
        oldPassword: "",
        newPassword: "",
        newPasswordAgain: "",
      },
      rules: {
        oldPassword: [
          {
            required: true,
            message: "请输入原密码",
            trigger: "blur",
          },
        ],
        newPassword: [
          { required: true, validator: validatePass1, trigger: "blur" },
        ],
        newPasswordAgain: [
          { required: true, validator: validatePass2, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
<style lang="less">
.amendWord {
  .el-input {
    width: 6.3rem;
  }
  .el-input__inner {
    height: 32px;
  }
  .el-input__suffix {
    display: none;
  }
  .el-button--info {
    width: 6.3rem;
    height: 32px;
  }
  .el-form-item {
    margin-bottom: 40px;
    &:last-child{
      margin-left: 100px;
    }
  }
}
</style>
<style lang='less' scoped>
.account-box {
  padding: 45px 25px 25px;

  .amendWord {
    width: 100%;
    height: 100%;
  }
}
</style>
